package com.atguigu.yygh.cmn.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.yygh.cmn.listener.DictListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {


    @Override
    public String getDictName(String dictCode, String value) {
      //如果dictCode为空  直接根据value查询
        if(StringUtils.isEmpty(dictCode)){
            QueryWrapper<Dict> wrapper = new QueryWrapper<>();
            wrapper.eq("value",value);
            Dict dict = baseMapper.selectOne(wrapper);
            return dict.getName();
        }else{
            Dict codeDict = this.getDictByDictCode(dictCode);
            Long parentid=codeDict.getId();

            QueryWrapper<Dict> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id",parentid);
            wrapper1.eq("value",value);
            Dict dict = baseMapper.selectOne(wrapper1);
            return dict.getName();

        }
    }

    private Dict getDictByDictCode(String dictCode){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code",dictCode);
        Dict codeDict = baseMapper.selectOne(wrapper);
        return codeDict;
    }


    @Override
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    public List<Dict> findChildData(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Dict> dictList = baseMapper.selectList(wrapper);
        for(Dict dict:dictList){
            Long id1=dict.getId();
            if(isChildren(id1)){
                dict.setHasChildren(true);
            }else{
                dict.setHasChildren(false);
            }
        }
        return dictList;
    }


    private  boolean  isChildren(Long id){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        Integer count = baseMapper.selectCount(wrapper);
       return  count>0;
    }

    @Override
    @CacheEvict(value = "dict",allEntries = true)
    public void exportDict(HttpServletResponse response) {

        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName="dict";
        response.setHeader("Content-disposition","attachment:filename="+fileName+".xlsx");

        List<DictEeVo> dictEeVoList=new ArrayList<>();
        List<Dict> dictList = baseMapper.selectList(null);
       for(Dict dict:dictList){
           DictEeVo dictEeVo = new DictEeVo();
           BeanUtils.copyProperties(dict,dictEeVo);
           dictEeVoList.add(dictEeVo);
       }


        try {
            EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet("dict").doWrite(dictEeVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    @CacheEvict(value = "dict", allEntries=true)
    public void importDict(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//根据dictcode查询下级节点
    @Override
    public List<Dict> findByDictCode(String dictCode) {
        //根据dictcode获取id
        Dict code = this.getDictByDictCode(dictCode);
        List<Dict> data = this.findChildData(code.getId());

        return data;
    }
}
